﻿using System.IO;
using System.Xml.Serialization;

namespace YanZhiwei.DotNet2.Utilities.Base
{
    /// <summary>
    /// XML文件数据库
    /// <para>参考：http://www.cnblogs.com/zfanlong1314/p/3620171.html </para>
    /// </summary>
    public class XmlDatabase : FileDatabase
    {
        /// <summary>
        /// XML文件数据库
        /// </summary>
        /// <param name="directory">数据库文件所在目录</param>
        public XmlDatabase(string directory)
            : base(directory)
        {
            FileExtension = @"xml";
        }

        /// <summary>
        /// 将指定的文档对象序列化至字符串
        /// </summary>
        /// <param name="value">指定的文档对象</param>
        /// <returns>
        /// 文档对象序列化后的字符串
        /// </returns>
        protected override string Serialize(object value)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer _serializer = new XmlSerializer(value.GetType());
                _serializer.Serialize(sw, value);
                return sw.ToString();
            }
        }

        /// <summary>
        /// 将字符串反序列化成文档对象
        /// </summary>
        /// <typeparam name="TDocument">文档类型</typeparam>
        /// <param name="data">字符串</param>
        /// <returns>
        /// 文档对象
        /// </returns>
        protected override TDocument Deserialize<TDocument>(string data)
        {
            using (StringReader sr = new StringReader(data))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TDocument));
                return (TDocument)serializer.Deserialize(sr);
            }
        }
    }
}
